﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Diagnostics;

namespace qna.Business {
	public class User : BusinessObject {

		public int Id { get; set; }
		public string Name { get; set; }
		public string Username { get; set; }
		public string Password { get; set; }
		public char Type { get; set; }

		public const char STUDENT_TYPE = 's';
		public const char LECTURER_TYPE = 'l';

		protected string JoinStatement {
			get {
				if (TableName == "[user]") {
					return "";
				} else {
					return " INNER JOIN " + TableName + " ON [user].id = " + TableName + "." + PrimaryKey;
				}
			}
		}

		protected override string TableName {
			get { return "[user]"; }
		}

		public override IDictionary<string, object> Attributes {
			get {
				IDictionary<string, object> attrs = new Dictionary<string, object>();
				attrs["Id"] = Id;
				attrs["Name"] = Name;
				attrs["Username"] = Username;
				attrs["Password"] = Password;
				attrs["Type"] = Type;
				return attrs;
			}
		}

		public override void ReadAttributes(SqlDataReader dr) {
			Id = (int)dr["Id"];
			Name = dr["Name"].ToString();
			Username = dr["Username"].ToString();
			Password = dr["Password"].ToString();
			Type = dr["Type"].ToString()[0];
		}

		public new IList<BusinessObject> GetAll(int limit = 0) {
			string query = "SELECT ";
			if (limit != 0)
				query += " TOP " + limit;
			query += " * FROM [user] " + JoinStatement;
			return QueryAll(query);
		}

		public new BusinessObject GetById(int id) {
			BusinessObject result = null;
			string sql = "SELECT * FROM [user] " + JoinStatement + " WHERE id = @id";
			IDictionary<string, object> param = new Dictionary<string, object>();
			param["@id"] = id;
			ExecuteReader(sql, param, delegate(SqlDataReader dr) {
				if (dr.Read()) {
					result = New();
					result.ReadAttributes(dr);
				}
				return dr;
			});			return result;
		}

		public User ValidateLogin(string user, string pass)
		{
			User result = null;
			string sql = "select * from [user] WHERE username=@user";
			IDictionary<string, object> param = new Dictionary<string, object>();
			param["@user"] = user;
			ExecuteReader(sql, param, delegate(SqlDataReader dr)
			{
				if (dr.Read())
				{
					result = (User)New();
					result.ReadAttributes(dr);
				}
				return dr;
			});
			if (user == result.Username && pass == result.Password.ToUpper())
				return result;
			else
				return null;
		}

        public override string UserName(string uname)
        {
            string username = "";
            string sql = "SELECT name FROM [User] WHERE username=@username";
            IDictionary<string, object> param = new Dictionary<string, object>();
            param.Add("@username", uname);
            ExecuteReader(sql, param, delegate(SqlDataReader dr)
            {
                if (dr.Read())
                {
                    username = (string)dr["name"];
                }
                return dr;
            });
            Debug.Write(username);
            return username;
        }
      
		public new int Insert(bool ignorePk = true) {
			throw new NotImplementedException("I don't think we need this. Not sure if base class method will work correctly.");
		}

		public new int Update(bool ignorePk = true) {
			throw new NotImplementedException("I don't think we need this. Not sure if base class method will work correctly.");
		}

		public new int Delete() {
			throw new NotImplementedException("I don't think we need this. Not sure if base class method will work correctly.");
		}

		protected override BusinessObject New()
		{
			return new User();
		}
	}
}